package guda.shop.cms.manager.impl;

import guda.shop.cms.dao.OrderDao;
import guda.shop.cms.entity.*;
import guda.shop.cms.manager.*;
import guda.shop.common.hibernate3.Updater;
import guda.shop.common.page.Pagination;
import guda.shop.core.entity.Website;
import guda.shop.core.manager.WebsiteMng;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class OrderMngImpl
        implements OrderMng {

    @Autowired
    private ProductMng productMng;

    @Autowired
    private ProductFashionMng productFashionMng;

    @Autowired
    private ShopScoreMng shopScoreMng;

    @Autowired
    private WebsiteMng websiteMng;

    @Autowired
    private ShopMemberMng shopMemberMng;

    @Autowired
    private CartMng cartMng;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private CartItemMng cartItemMng;

    @Autowired
    private GatheringMng gatheringMng;

    @Autowired
    private ShipmentsMng shipmentsMng;

    @Autowired
    private OrderReturnMng orderReturnMng;

    @Autowired
    private MemberCouponMng memberCouponMng;

    @Autowired
    private PaymentMng paymentMng;

    @Autowired
    private ShopMemberAddressMng shopMemberAddressMng;

    @Autowired
    private ShippingMng shippingMng;

    @Autowired
    private PopularityItemMng popularityItemMng;

    public Pagination getPageForOrderReturn(Long paramLong, int paramInt1, int paramInt2) {
        return this.orderDao.getPageForOrderReturn(paramLong, paramInt1, paramInt2);
    }

    public Order createOrder(Cart paramCart, Long[] paramArrayOfLong, Long paramLong1, Long paramLong2, Long paramLong3, String paramString1, String paramString2, ShopMember paramShopMember, Long paramLong4, String paramString3) {
        Website localWebsite = this.websiteMng.findById(paramLong4);
        Long localLong1 = null;
        if (!StringUtils.isBlank(paramString3))
            localLong1 = Long.valueOf(Long.parseLong(paramString3));
        Payment localPayment = this.paymentMng.findById(paramLong3);
        Shipping localShipping = this.shippingMng.findById(paramLong1);
        ShopMemberAddress localShopMemberAddress = this.shopMemberAddressMng.findById(paramLong2);
        Order localOrder = new Order();
        localOrder.setShipping(localShipping);
        localOrder.setWebsite(localWebsite);
        localOrder.setMember(paramShopMember);
        localOrder.setPayment(localPayment);
        localOrder.setIp(paramString2);
        localOrder.setComments(paramString1);
        localOrder.setStatus(Integer.valueOf(1));
        localOrder.setShippingStatus(Integer.valueOf(1));
        localOrder.setPaymentStatus(Integer.valueOf(1));
        localOrder.setReceiveName(localShopMemberAddress.getUsername());
        localOrder.setReceiveAddress(localShopMemberAddress.getDetailaddress());
        localOrder.setReceiveMobile(localShopMemberAddress.getTel());
        localOrder.setReceivePhone(localShopMemberAddress.getMobile());
        localOrder.setReceiveZip(localShopMemberAddress.getPostCode());
        int i = 0;
        int j = 0;
        double d1 = 0.0D;
        Double localDouble1 = Double.valueOf(0.0D);
        Double localDouble2 = Double.valueOf(0.0D);
        Object localObject1;
        Iterator<PopularityItem> localObject2;
        if (paramCart != null) {
            localObject1 = this.popularityItemMng.getlist(paramCart.getId(), null);
            localObject2 = ((List) localObject1).iterator();
            while (((Iterator) localObject2).hasNext()) {
                PopularityItem localPopularityItem = (PopularityItem) ((Iterator) localObject2).next();
                localDouble2 = Double.valueOf(localDouble2.doubleValue() + localPopularityItem.getPopularityGroup().getPrice().doubleValue() * localPopularityItem.getCount().intValue());
            }
        }
        if (localLong1 != null) {
            localObject1 = this.memberCouponMng.findById(localLong1);
            if ((localObject1 != null) && (((MemberCoupon) localObject1).getCoupon().getIsusing().booleanValue()) && (!((MemberCoupon) localObject1).getIsuse().booleanValue())) {
                localDouble1 = Double.valueOf(((MemberCoupon) localObject1).getCoupon().getCouponPrice().doubleValue());
                ((MemberCoupon) localObject1).setIsuse(Boolean.valueOf(true));
                this.memberCouponMng.update((MemberCoupon) localObject1);
            }
        }
        localObject1 = new ArrayList();
        for (Object localObject3 : paramArrayOfLong)
            ((List) localObject1).add(this.cartItemMng.findById((Long) localObject3));
        localObject2 = ((List) localObject1).iterator();
        while (((Iterator) localObject2).hasNext()) {
            CartItem localCartItem = (CartItem) ((Iterator) localObject2).next();
            i += localCartItem.getProduct().getScore().intValue() * localCartItem.getCount().intValue();
            j += localCartItem.getProduct().getWeight().intValue() * localCartItem.getCount().intValue();
            if (localCartItem.getProductFash() != null)
                d1 += localCartItem.getProductFash().getSalePrice().doubleValue() * localCartItem.getCount().intValue();
            else
                d1 += localCartItem.getProduct().getSalePrice().doubleValue() * localCartItem.getCount().intValue();
        }
        if (paramShopMember.getFreezeScore() != null)
            paramShopMember.setFreezeScore(Integer.valueOf(i + paramShopMember.getFreezeScore().intValue()));
        else
            paramShopMember.setFreezeScore(Integer.valueOf(i + 0));
        localOrder.setScore(Integer.valueOf(i));
        localOrder.setWeight(Double.valueOf(j));
        localOrder.setProductPrice(Double.valueOf(d1));
        double d2 = localShipping.calPrice(Double.valueOf(j)).doubleValue();
        localOrder.setFreight(Double.valueOf(d2));
        localOrder.setTotal(Double.valueOf(d2 + d1 - localDouble1.doubleValue() - localDouble2.doubleValue()));
        Long localLong2 = Long.valueOf(new Date().getTime() + paramShopMember.getId().longValue());
        localOrder.setCode(localLong2);
        Object localObject3 = null;
        OrderItem localOrderItem1 = null;
        String str = null;
        for (int n = 0; n < ((List) localObject1).size(); n++) {
            localOrderItem1 = new OrderItem();
            localObject3 = (CartItem) ((List) localObject1).get(n);
            localOrderItem1.setOrdeR(localOrder);
            localOrderItem1.setProduct(((CartItem) localObject3).getProduct());
            localOrderItem1.setWebsite(localWebsite);
            localOrderItem1.setCount(((CartItem) localObject3).getCount());
            if (((CartItem) localObject3).getProductFash() != null) {
                localOrderItem1.setProductFash(((CartItem) localObject3).getProductFash());
                localOrderItem1.setSalePrice(((CartItem) localObject3).getProductFash().getSalePrice());
            } else {
                localOrderItem1.setSalePrice(((CartItem) localObject3).getProduct().getSalePrice());
            }
            str = str + localOrderItem1.getProduct().getName();
            localOrder.addToItems(localOrderItem1);
        }
        localOrder.setProductName(str);
        List localList = this.popularityItemMng.getlist(paramCart.getId(), null);
        Object localObject4 = localList.iterator();
        while (((Iterator) localObject4).hasNext()) {
            Object localObject5 = (PopularityItem) ((Iterator) localObject4).next();
            this.popularityItemMng.deleteById(((PopularityItem) localObject5).getId());
        }
        paramCart.getItems().removeAll((Collection) localObject1);
        this.cartMng.update(paramCart);
        localOrder = save(localOrder);
        localObject4 = null;
        Object localObject5 = null;
        ProductFashion localProductFashion = null;
        Iterator localIterator = localOrder.getItems().iterator();
        while (localIterator.hasNext()) {
            OrderItem localOrderItem2 = (OrderItem) localIterator.next();
            localObject5 = localOrderItem2.getProduct();
            if (localOrderItem2.getProductFash() != null) {
                localProductFashion = localOrderItem2.getProductFash();
                localProductFashion.setStockCount(Integer.valueOf(localProductFashion.getStockCount().intValue() - localOrderItem2.getCount().intValue()));
                ((Product) localObject5).setStockCount(Integer.valueOf(((Product) localObject5).getStockCount().intValue() - localOrderItem2.getCount().intValue()));
                this.productFashionMng.update(localProductFashion);
            } else {
                ((Product) localObject5).setStockCount(Integer.valueOf(((Product) localObject5).getStockCount().intValue() - localOrderItem2.getCount().intValue()));
            }
            this.productMng.updateByUpdater((Product) localObject5);
            localObject4 = new ShopScore();
            ((ShopScore) localObject4).setMember(paramShopMember);
            ((ShopScore) localObject4).setName(((CartItem) localObject3).getProduct().getName());
            ((ShopScore) localObject4).setScoreTime(new Date());
            ((ShopScore) localObject4).setStatus(false);
            ((ShopScore) localObject4).setUseStatus(false);
            ((ShopScore) localObject4).setScoreType(Integer.valueOf(ShopScore.ScoreTypes.ORDER_SCORE.getValue()));
            ((ShopScore) localObject4).setScore(localOrderItem2.getProduct().getScore());
            ((ShopScore) localObject4).setCode(Long.toString(localOrder.getCode()));
            this.shopScoreMng.save((ShopScore) localObject4);
        }
        return localOrder;
    }

    public List<Order> getlistByforaddressId(Long paramLong) {
        return this.orderDao.getlistByforaddressId(paramLong);
    }

    public Order updateByUpdater(Order paramOrder) {
        Updater localUpdater = new Updater(paramOrder);
        return this.orderDao.updateByUpdater(localUpdater);
    }

    public Pagination getOrderByReturn(Long paramLong, Integer paramInteger1, Integer paramInteger2) {
        return this.orderDao.getOrderByReturn(paramLong, paramInteger1, paramInteger2);
    }

    @Transactional(readOnly = true)
    public Pagination getPage(Long paramLong1, Long paramLong2, String paramString1, String paramString2, Long paramLong3, Long paramLong4, Date paramDate1, Date paramDate2, Double paramDouble1, Double paramDouble2, Integer paramInteger1, Integer paramInteger2, Integer paramInteger3, Long paramLong5, int paramInt1, int paramInt2) {
        Pagination localPagination = this.orderDao.getPage(paramLong1, paramLong2, paramString1, paramString2, paramLong3, paramLong4, paramDate1, paramDate2, paramDouble1, paramDouble2, paramInteger1, paramInteger2, paramInteger3, paramLong5, paramInt1, paramInt2);
        return localPagination;
    }

    public List<Order> getlist() {
        SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date localDate1 = new Date();
        Date localDate2 = null;
        Calendar localCalendar = Calendar.getInstance();
        localCalendar.setTime(localDate1);
        localCalendar.set(5, localCalendar.get(5) - 1);
        try {
            localDate2 = localSimpleDateFormat.parse(localSimpleDateFormat.format(localCalendar.getTime()));
        } catch (ParseException localParseException) {
            localParseException.printStackTrace();
        }
        List localList = this.orderDao.getlist(localDate2);
        return localList;
    }

    public void abolishOrder() {
        List localList = getlist();
        Iterator localIterator = localList.iterator();
        while (localIterator.hasNext()) {
            Order localOrder = (Order) localIterator.next();
            localOrder.setStatus(Integer.valueOf(3));
            Object localObject1 = localOrder.getItems().iterator();
            Object localObject4;
            while (((Iterator) localObject1).hasNext()) {
                OrderItem localObject2 = (OrderItem) ((Iterator) localObject1).next();
                Product localObject3 = ((OrderItem) localObject2).getProduct();
                if (((OrderItem) localObject2).getProductFash() != null) {
                    localObject4 = ((OrderItem) localObject2).getProductFash();
                    ((ProductFashion) localObject4).setStockCount(Integer.valueOf(((ProductFashion) localObject4).getStockCount().intValue() + ((OrderItem) localObject2).getCount().intValue()));
                    ((Product) localObject3).setStockCount(Integer.valueOf(((Product) localObject3).getStockCount().intValue() + ((OrderItem) localObject2).getCount().intValue()));
                    this.productFashionMng.update((ProductFashion) localObject4);
                } else {
                    ((Product) localObject3).setStockCount(Integer.valueOf(((Product) localObject3).getStockCount().intValue() + ((OrderItem) localObject2).getCount().intValue()));
                }
                this.productMng.updateByUpdater((Product) localObject3);
            }
            localObject1 = localOrder.getMember();
            ((ShopMember) localObject1).setFreezeScore(Integer.valueOf(((ShopMember) localObject1).getFreezeScore().intValue() - localOrder.getScore().intValue()));
            this.shopMemberMng.update((ShopMember) localObject1);
            Object localObject2 = this.shopScoreMng.getlist(Long.toString(localOrder.getCode()));
            Object localObject3 = ((List) localObject2).iterator();
            while (((Iterator) localObject3).hasNext()) {
                localObject4 = (ShopScore) ((Iterator) localObject3).next();
                this.shopScoreMng.deleteById(((ShopScore) localObject4).getId());
            }
            updateByUpdater(localOrder);
        }
    }

    @Transactional(readOnly = true)
    public Pagination getPage(Long paramLong1, Long paramLong2, String paramString1, String paramString2, Long paramLong3, Long paramLong4, Date paramDate1, Date paramDate2, Double paramDouble1, Double paramDouble2, Integer paramInteger, Long paramLong5, int paramInt1, int paramInt2) {
        Pagination localPagination = this.orderDao.getPage(paramLong1, paramLong2, paramString1, paramString2, paramLong3, paramLong4, paramDate1, paramDate2, paramDouble1, paramDouble2, paramInteger, paramLong5, paramInt1, paramInt2);
        return localPagination;
    }

    @Transactional(readOnly = true)
    public Pagination getPageForMember(Long paramLong, int paramInt1, int paramInt2) {
        return this.orderDao.getPageForMember(paramLong, paramInt1, paramInt2);
    }

    public Pagination getPageForMember1(Long paramLong, int paramInt1, int paramInt2) {
        return this.orderDao.getPageForMember1(paramLong, paramInt1, paramInt2);
    }

    public Pagination getPageForMember2(Long paramLong, int paramInt1, int paramInt2) {
        return this.orderDao.getPageForMember2(paramLong, paramInt1, paramInt2);
    }

    public Pagination getPageForMember3(Long paramLong, int paramInt1, int paramInt2) {
        return this.orderDao.getPageForMember3(paramLong, paramInt1, paramInt2);
    }

    @Transactional(readOnly = true)
    public Order findById(Long paramLong) {
        Order localOrder = this.orderDao.findById(paramLong);
        return localOrder;
    }

    public void updateSaleCount(Long paramLong) {
        Order localOrder = this.orderDao.findById(paramLong);
        Iterator localIterator = localOrder.getItems().iterator();
        while (localIterator.hasNext()) {
            OrderItem localOrderItem = (OrderItem) localIterator.next();
            Product localProduct = localOrderItem.getProduct();
            localProduct.setSaleCount(Integer.valueOf(localProduct.getSaleCount().intValue() + localOrderItem.getCount().intValue()));
            this.productMng.update(localProduct);
        }
    }

    public void updateliRun(Long paramLong) {
        Order localOrder = this.orderDao.findById(paramLong);
        Iterator localIterator = localOrder.getItems().iterator();
        while (localIterator.hasNext()) {
            OrderItem localOrderItem = (OrderItem) localIterator.next();
            Product localProduct = localOrderItem.getProduct();
            ProductFashion localProductFashion = localOrderItem.getProductFash();
            if (localProductFashion != null)
                localProduct.setLiRun(Double.valueOf(localProduct.getLiRun().doubleValue() + localOrderItem.getCount().intValue() * (localProductFashion.getSalePrice().doubleValue() - localProductFashion.getCostPrice().doubleValue())));
            else
                localProduct.setLiRun(Double.valueOf(localProduct.getLiRun().doubleValue() + localOrderItem.getCount().intValue() * (localProduct.getSalePrice().doubleValue() - localProduct.getCostPrice().doubleValue())));
            this.productMng.update(localProduct);
        }
    }

    public Order save(Order paramOrder) {
        paramOrder.init();
        this.orderDao.save(paramOrder);
        return paramOrder;
    }

    public List<Object> getTotlaOrder() {
        return this.orderDao.getTotlaOrder();
    }

    public BigDecimal getMemberMoneyByYear(Long paramLong) {
        return this.orderDao.getMemberMoneyByYear(paramLong);
    }

    public Order deleteById(Long paramLong) {
        Order localOrder = this.orderDao.findById(paramLong);
        this.gatheringMng.deleteByorderId(paramLong);
        this.shipmentsMng.deleteByorderId(paramLong);
        if (localOrder.getReturnOrder() != null)
            this.orderReturnMng.deleteById(localOrder.getReturnOrder().getId());
        localOrder = this.orderDao.deleteById(paramLong);
        return localOrder;
    }

    public Order[] deleteByIds(Long[] paramArrayOfLong) {
        Order[] arrayOfOrder = new Order[paramArrayOfLong.length];
        int i = 0;
        int j = paramArrayOfLong.length;
        while (i < j) {
            arrayOfOrder[i] = deleteById(paramArrayOfLong[i]);
            i++;
        }
        return arrayOfOrder;
    }
}

